import { BaseModel } from './BaseModel.js';
import { dataFiles } from '../config/database.js';

// 默认角色权限关联
// admin拥有所有权限（1-21）
// teacher拥有：查看学生(1)、查看成绩(11)、管理成绩(12)、查看考勤(13)、管理考勤(14)、查看仪表盘(16)、使用视频会议(20)、创建会议(21)
// student拥有：查看仪表盘(16)、查看自己的成绩和考勤（在controller中控制）

const defaultRolePermissions = [
    // 超级管理员拥有所有权限
    { id: 1, roleId: 1, permissionId: 1 },  // admin -> student:read
    { id: 2, roleId: 1, permissionId: 2 },  // admin -> student:create
    { id: 3, roleId: 1, permissionId: 3 },  // admin -> student:update
    { id: 4, roleId: 1, permissionId: 4 },  // admin -> student:delete
    { id: 5, roleId: 1, permissionId: 5 },  // admin -> student:export
    { id: 6, roleId: 1, permissionId: 6 },  // admin -> student:import
    { id: 7, roleId: 1, permissionId: 7 },  // admin -> teacher:read
    { id: 8, roleId: 1, permissionId: 8 },  // admin -> teacher:create
    { id: 9, roleId: 1, permissionId: 9 },  // admin -> teacher:update
    { id: 10, roleId: 1, permissionId: 10 }, // admin -> teacher:delete
    { id: 11, roleId: 1, permissionId: 11 }, // admin -> grade:read
    { id: 12, roleId: 1, permissionId: 12 }, // admin -> grade:manage
    { id: 13, roleId: 1, permissionId: 13 }, // admin -> attendance:read
    { id: 14, roleId: 1, permissionId: 14 }, // admin -> attendance:manage
    { id: 15, roleId: 1, permissionId: 15 }, // admin -> class:manage
    { id: 16, roleId: 1, permissionId: 16 }, // admin -> dashboard:view
    { id: 17, roleId: 1, permissionId: 17 }, // admin -> user:manage
    { id: 18, roleId: 1, permissionId: 18 }, // admin -> system:config
    { id: 19, roleId: 1, permissionId: 19 }, // admin -> role:manage
    { id: 20, roleId: 1, permissionId: 20 }, // admin -> video:use
    { id: 21, roleId: 1, permissionId: 21 }, // admin -> video:create

    // 教师角色权限
    { id: 22, roleId: 2, permissionId: 1 },  // teacher -> student:read
    { id: 23, roleId: 2, permissionId: 11 }, // teacher -> grade:read
    { id: 24, roleId: 2, permissionId: 12 }, // teacher -> grade:manage
    { id: 25, roleId: 2, permissionId: 13 }, // teacher -> attendance:read
    { id: 26, roleId: 2, permissionId: 14 }, // teacher -> attendance:manage
    { id: 27, roleId: 2, permissionId: 16 }, // teacher -> dashboard:view
    { id: 28, roleId: 2, permissionId: 20 }, // teacher -> video:use
    { id: 29, roleId: 2, permissionId: 21 }, // teacher -> video:create

    // 学生角色权限
    { id: 30, roleId: 3, permissionId: 16 }, // student -> dashboard:view
    { id: 31, roleId: 3, permissionId: 20 }  // student -> video:use
];

export class RolePermission extends BaseModel {
    constructor() {
        super(dataFiles.rolePermissions, defaultRolePermissions);
    }

    findByRoleId(roleId) {
        return this.findAll().filter(rp => rp.roleId === roleId);
    }

    findByPermissionId(permissionId) {
        return this.findAll().filter(rp => rp.permissionId === permissionId);
    }

    getRolePermissions(roleId) {
        return this.findByRoleId(roleId);
    }

    addRolePermission(roleId, permissionId) {
        const existing = this.findOne({ roleId, permissionId });
        if (existing) {
            return existing;
        }
        return this.create({ roleId, permissionId });
    }

    removeRolePermission(roleId, permissionId) {
        const item = this.findOne({ roleId, permissionId });
        if (item) {
            return this.delete(item.id);
        }
        return false;
    }

    removeAllRolePermissions(roleId) {
        const rolePermissions = this.findByRoleId(roleId);
        const count = rolePermissions.length;
        rolePermissions.forEach(rp => this.delete(rp.id));
        console.log(`移除了角色 ${roleId} 的 ${count} 个权限`);
        return count > 0;
    }
}

